#!/usr/bin/perl
#save_create.cgi

require './diskman-lib.pl';

&ReadParse();

my ($err_msg, $log_ev, $log_msg, $log_pri);

$err_msg="";
$log_ev="";

my $mkfs_lock;
$mkfs_lock = check_lock();
if ($mkfs_lock) {
	$err_msg = $text{"err_mkfslocked"};
	goto ERROR_OUT;
}

my ($level, $disklist, $volname, $needspare, $sparedisk);

$level = $in{'level'};
$disklist = $in{'listinput'};
$volname = $in{'volname'};
$needspare = $in{'needspare'};
$sparedisk = $in{'sparedisk'};

my (@rdinfo, @dinfo, @free_volumes, @free_disks, $i);

@rdinfo =  get_check_raid_info();
@dinfo = get_diskinfo(\@rdinfo);

@free_volumes = &get_free_volumes(\@rdinfo);
@free_disks = &get_free_disks(\@dinfo);

my ($fv, $found);
$found = 0;
foreach $fv (@free_volumes) {
	if ($fv eq $volname) {
		$found = 1;
		last;
	}
}

if (!$found) {
	$err_msg = $text{"err_configchanged"};
	goto ERROR_OUT;
}

my (@diskname, $dn, $errdisk);
@diskname = split(/\s+/, $disklist);
$errdisk = 0;
#`echo $disklist >>/root/niu`;
my ($sparedev,$index);
if ($needspare =~ /needspare/) {
	if (index(join(' ', @free_disks), $sparedisk) < 0) {
		$sparedev = undef;
		$errdisk = 1;
	}
	else {
		$sparedisk =~ /disk(\d+)/;
		$index=$1-1;
		$sparedev = $dinfo[$index]{'device'};
	}
	$sparedev = 1;
}
else {
	#$sparedev = undef;
	$sparedev = 0;
}

my (@rdevs);
foreach $dn (@diskname) {
	$dn =~ /disk(\d+)/;
	$index=$1-1;

	if (index(join(' ', @free_disks), $dn) < 0) {
		$errdisk = 1;
		last;
	}
	if ($needspare =~ /needspare/) {
		if ($dn ne $sparedisk) {
			push(@rdevs, $dinfo[$index]{'device'});
		}
	}
	else {
		push(@rdevs, $dinfo[$index]{'device'});
	}
}
`rm -f /root/cmc`;
foreach (@rdevs) {
#	`echo -$_ >> /root/cmc`;
}
#`echo $needspare >> /root/cmc`;
#$err_msg = @rdevs;
#goto ERROR_OUT;
if ($errdisk) {
	$err_msg = $text{"err_configchanged"};
	goto ERROR_OUT;
}

if ($level eq 'linear') {
	if (@rdevs < 1) {
		$err_msg = $text{"err_cgiparam"};
		goto ERROR_OUT;
	}
}
elsif ($level == 0) {
	if (@rdevs < 1) {
		$err_msg = $text{"err_cgiparam"};
		goto ERROR_OUT;
	}
}
elsif ($level == 1) {
	if (@rdevs < 2) {
		$err_msg = $text{"err_cgiparam"};
		goto ERROR_OUT;
	}
}
elsif ($level == 5) {
	if (@rdevs < 3) {
		$err_msg = $text{"err_cgiparam"};
		goto ERROR_OUT;
	}
}
elsif ($level == 6) {
        if (@rdevs < 4) {
                $err_msg = $text{"err_cgiparam"};
                goto ERROR_OUT;
        }
}
else {
	$err_msg = $text{"err_cgiparam"};
	goto ERROR_OUT;
}

# check finished
my ($mddev);
$volname =~ /\S+(\d+)/;
$mddev = $1 + $hwinfo{'uraidstart'} - 1;
$mddev = '/dev/md'.$mddev;
my $rdisklist = join(" ", sort(@rdevs));
#$err_msg = $rdisklist;
#goto ERROR_OUT;
my ($rc);
#$rc = create_volume($level, \@rdevs, $volname, $mddev, $sparedev);
$rc = create_volume($level, $rdisklist, $volname, $mddev, $sparedev);
if ($rc) {
	$log_ev = $text{'log_ev_crtvolume_err'};
	$log_msg= &text('log_msg_crtvolume_err', $volname, 'RAID '.$level);
	nas_syslog($text{'my_log_minor'}, 'naslog_warning', $log_ev, $log_msg);

	#$err_msg = $rc.$text{'err_crtvol'};
	#goto ERROR_OUT;
}

$log_ev = $text{'log_ev_crtvolume_ok'};
$log_msg= &text('log_msg_crtvolume_ok', $volname, 'RAID '.$level);
nas_syslog($text{'my_log_minor'}, 'naslog_info', $log_ev, $log_msg);
$out = `/sbin/lvm pvcreate $mddev`;
&redirect("index.cgi");
ERROR_OUT:
&error_setup($text{'crt_err_title'});
&error($err_msg);
#&redirect("");


